package vtt.android.docbaovn.database;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import vtt.android.docbaovn.entities.RssSource;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class RssSourceDAO {


	// field database
	public static String TABLE_NAME = "rss_source";

	public static final String COL_ID = "id";
	public static final String COL_PAGE_CODE = "page_code";
	public static final String COL_CATEGORY_TYPE = "category_type";
	public static final String COL_LINK = "link";
	
	// Database fields
	private SQLiteDatabase db;
	private MySQLiteOpenHelper dbHelper;


	public RssSourceDAO(Context context) {
		dbHelper = new MySQLiteOpenHelper(context);
	}

	public void open() throws SQLException {
		if(db == null) {
			db = dbHelper.getWritableDatabase();
		}
	}

	public void close() {
		dbHelper.close();
	}

//	/**
//	 * All CRUD(Create, Read, Update, Delete) Operations
//	 */
//
//	// Adding new Customer
//	void addCustomer(Customer customer) {
//		ContentValues values = new ContentValues();
//		values.put(COL_FULL_NAME_KANA, customer.fullnamekana); // Customer
//		values.put(COL_PHONE_NUMBER, customer.phonenumber); // Customer
//
//		// Inserting Row
//		db.insert(TABLE_NAME, null, values);
//		db.close(); // Closing database connection
//	}
//
//	// Getting single Customer
//	Customer getCustomer(int id) {
//		Cursor cursor = db.query(TABLE_NAME, new String[] { 
//					COL_MEMBER_NO, 
//					COL_FULL_NAME_KANA, 
//					COL_FULL_NAME_KANJI 
//				}, 
//				COL_MEMBER_NO + "=?",
//				new String[] { String.valueOf(id) }, 
//				null, 
//				null, 
//				null, 
//				null);
//		
//		if (cursor != null)
//			cursor.moveToFirst();
//
////		Customer Customer = new Customer(cursor.getString(0),cursor.getString(1),cursor.getString(2),"",null);
//		// return Customer
//		
//		cursor.close();
////		return Customer;
//		return null;
//	}
//	
//
	public List<RssSource> getRssSource(boolean isActive) {
		List<RssSource> rssList = new ArrayList<RssSource>();
		// Select All Query
		String selectQuery = 
				" SELECT  id, page_code, category_type, link, active" +
				" FROM " + TABLE_NAME +  
				" WHERE active = " + ( isActive == true ? "'1'" : "'0'")  ;

		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				RssSource rssSource = new RssSource();
				rssSource.id = cursor.getString(0);
				rssSource.page_code = cursor.getString(1);
				rssSource.category_type = cursor.getString(2);
				rssSource.link = cursor.getString(3);
				rssSource.active = cursor.getString(4);
				// Adding Customer to list
				rssList.add(rssSource);
			} while (cursor.moveToNext());
		}

		// return Customer list
		return rssList;
	}
	
	public JSONArray getRssListByCategory(String categoryType) {
		JSONArray rssList = new JSONArray();
		// Select All Query
		String selectQuery = 
				" SELECT  link " +
				" FROM " + TABLE_NAME +  
				" WHERE active = '1' and category_type = '" + categoryType + "'"  ;

		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				String link = cursor.getString(0);
				// Adding Customer to list
				rssList.put(link);
			} while (cursor.moveToNext());
		}

		return rssList;
	}
	
	/**
	 * get rss url list
	 * @param categoryType : loai chuyen muc
	 * @return list of string rss url
	 */
	public List<String> getRssListString(String categoryType) {
		List<String> rssList = new ArrayList<String>();
		// Select All Query
		String selectQuery = 
				" SELECT  link " +
				" FROM " + TABLE_NAME +  
				" WHERE active = '1' and category_type = '" + categoryType + "'"  ;

		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				String link = cursor.getString(0);
				// Adding Customer to list
				rssList.add(link);
			} while (cursor.moveToNext());
		}

		return rssList;
	}
//
//	// Updating single Customer
//	public int updateCustomer(Customer customer) {
//		ContentValues values = new ContentValues();
//		values.put(COL_FULL_NAME_KANA, customer.fullnamekana);
//		values.put(COL_FULL_NAME_KANJI, customer.fullnamekanji);
//		// updating row
//		return db.update(TABLE_NAME, values, COL_MEMBER_NO + " = ?", new String[] { customer.memberno });
//	}
//
//	// Deleting single Customer
//	public void deleteCustomer(Customer customer) {
//		db.delete(TABLE_NAME, COL_MEMBER_NO + " = ?", new String[] { customer.memberno });
//	}
//
//	// Getting Customers Count
//	public int getCustomersCount() {
//		String countQuery = "SELECT  * FROM " + TABLE_NAME;
//		Cursor cursor = db.rawQuery(countQuery, null);
//		cursor.close();
//		// return count
//		return cursor.getCount();
//	}
}
